package com.tsn.service.impl;

import com.tsn.common.Result;
import com.tsn.entity.Comment;
import com.tsn.mapper.CommentMapper;
import com.tsn.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author tsn
 * @since 2021-08-13
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Result getComment(Integer type, Integer id) {
        return Result.success(commentMapper.getCommentByType(type,id));
    }

    @Override
    public Result getForumComment(Integer forumId) {
        List<Comment> commentList = commentMapper.getCommentByType(0, forumId);
        Collections.reverse(commentList);
        for(Comment comment:commentList){
            if(comment.getParentId()!=null) {
                if (comment.getParentId() != 0) {
                    for(Comment comment1:commentList){
                        if(comment.getParentId()== comment1.getId()){
                            comment.setComment(comment1);
                        }
                    }

                }
            }
        }
//        commentList.forEach(System.out::println);
        return Result.success(commentList);
    }

}
